package marksys.free.com.marksys.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import marksys.free.com.marksys.R;
import marksys.free.com.marksys.nettool.bean.Bean;


/**
 * Created by zlinsun on 2018/8/2.
 * hah
 */
public class MyCameraListAdapter extends RecyclerView.Adapter<MyCameraListAdapter.ViewHolder> {
    private List list;
    OnRecyclerViewItemClickListener onItemClickListener;
    private LayoutInflater mInflater;

    public MyCameraListAdapter(Context context, List mData) {
        this.mInflater = LayoutInflater.from(context);
        this.list = mData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =  mInflater.inflate(R.layout.camera_list_item, parent, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onItemClickListener != null) {
                    //注意这里使用getTag方法获取数据
                    onItemClickListener.onItemClick(v, (Integer) v.getTag());
                }

            }
        });

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        final Bean model = (Bean) list.get(position);

        holder.itemView.setTag(position);

        holder.name.setText((CharSequence) model.name);

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //define interface
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int object);
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public TextView resNum;
        public TextView num;
        public ImageView minusmeal;
        public ImageView plusmeal;


        public ViewHolder(View itemView) {
            super(itemView);
           name = itemView.findViewById(R.id.name);
//            resNum = itemView.findViewById(R.id.resnum);
//            num = itemView.findViewById(R.id.num);
//            minusmeal = itemView.findViewById(R.id.minusmeal);
//            plusmeal = itemView.findViewById(R.id.plusmeal);
        }

    }
}